package com.smart.carey.model;

import java.util.HashMap;
import java.util.Map;

public class StandardDatagram {


    //参数类型错误 (业务逻辑错误，需要前端业务处理)
    public final static String  PARAM_VALIDATION_ERROR01 = "PVE0X000000001";
    //参数类型错误（参数逻辑验证错误）
    public final static String  PARAM_VALIDATION_ERROR02 = "PVE0X000000002";

    public StandardDatagram(){

    }

    private Integer status;
    private String message;
    private Map<String, Object> body;

    public Integer getStatus() {
        return status;
    }

    public String getMessage() {
        return message;
    }

    public Map<String, Object> getBody() {
        return body;
    }

    public StandardDatagram(Builder builder) {
        this.status = builder.status;
        this.message = builder.message;
        this.body = builder.body;

    }


    public static class Builder {


        private final static String SUCCEED_MSG = "Request succeeded ✓ ";
        private final static String FAILED_MSG = "Request failed ↻";
        private Integer status;
        private String message;
        private Map<String, Object> body;

        public StandardDatagram build() {
            return new StandardDatagram(this);

        }


        /**
         * 成功报文返回
         *
         * @return
         */
        public Builder createSuccess() {
            //请求成功返回报文
            this.message = SUCCEED_MSG;
            this.status = 0;
            return this;
        }


        /**
         * 成功报文返回
         *
         * @return
         */
        public Builder createFailed() {
            //请求成功返回报文
            this.message = FAILED_MSG;
            this.status = -1;
            return this;
        }


        /**
         * 存放数据
         *
         * @param data
         * @return
         */
        public Builder setData(Object data) {
            Map<String, Object> map = new HashMap<>();
            map.put("data", data);
            this.body = map;
            return this;
        }

        /**
         * 存放异常信息
         *
         * @param msg
         * @return
         */
        public Builder setException(String msg) {
            if(this.body == null){
                this.body = new HashMap<>();
            }
            this.body .put("exception", msg);
            return this;
        }

            public Builder setErrCode(String errCode) {
                if(this.body == null){
                    this.body = new HashMap<>();
                }
                this.body.put("errCode",errCode);
                return this;
            }

        }


}
